var getLastsel2,getAllIds;
require(['date','validator','grid'], function () {
	var selLineId,selLineName,corpIds,nodes,lastsel3,lastsel2,rowDatas,selLineIds,ids,vrsnCdHtmls;
	    $lineParameterSetWorkList = $("#lineParameterSetWorkList"),$lineParameterSetList = $("#lineParameterSetList"),
		$saveSet = $("#saveSet"),$formData=$("#formData"),$editAll=$("#editAll"),$cancleAll = $("#cancleAll");

		function gridInit() {
	    	debugger
			$lineParameterSetWorkList.grid({
				url :"/operationReport/searchStationBehaviorPage.htm",
				colModel:[
					{name: "id", label: "id", key: true, hidden: true},
					{name: "stnSn", label: "站点序号",align: "right"},
					{name: "lineType", label: "方向",dict: "LIT00000",align: "left"},
					{name: "stnName", label: "站点名称",align: "left"},
					{name: "aheadStnDura", label: "越站时长(S)",align: "right",editable:true,  edittype:'text',editoptions:{size:6,maxlength:5},editrules:{required:true}},
					{name: "stayStnDura", label: "滞站时长(S)",align: "right",editable:true, edittype:'text',editoptions:{size:6,maxlength:5}, editrules:{required:true}},
					{name: "vrsnCd", label: "版本编号",hidden: true},
					{name: "stnCd", label: "站点编号",hidden: true},
					{name: "lineType", label: "lineTypeStatus",hidden: true}
				],
				onSelectRow: function(id){
					debugger
					if(id && id!==lastsel2){
						jQuery('#lineParameterSetWorkList').grid('saveRow',lastsel2);
						jQuery('#lineParameterSetWorkList').grid('editRow',id,true);
						lastsel2=id;
						$("#carId").val(id);
					}
				},
				multiselect: false,
				rowNum:1000,
				height:$(window).height()-154,
				showPager: false,
				gridComplete: function () {
					if($("#forAddOrEdit").val()=="add"){
						var overStation = $("#overStation").val().trim();
						if(overStation) {
							if (!/^\d+$/.test(overStation)) {
								overStation=10;
							}
						}
						var stayStation = $("#stayStation").val().trim();
						if(stayStation) {
							if (!/^\d+$/.test(stayStation)) {
								stayStation=90;
							}
						}
						var rowIds = jQuery("#lineParameterSetList").jqGrid('getDataIDs');
						for (var i = 0; i < rowIds.length; i ++) {
							jQuery("#lineParameterSetList").jqGrid('setCell',rowIds[i], 'vrsnName', $("#versionName").val());
							jQuery("#lineParameterSetList").jqGrid('setCell',rowIds[i], 'strtusDate', $("#strtusDateStationEnable").val());
							jQuery("#lineParameterSetList").jqGrid('setCell',rowIds[i], 'aheadStnDura', overStation);
							jQuery("#lineParameterSetList").jqGrid('setCell',rowIds[i], 'stayStnDura', stayStation);
						}

					}
				}
			});
	    	
	    }
		getLastsel2 = function(lastsel2,rowData,selLineId,corpId,vrsnCdHtml,flag,lineName){
			debugger
			gridInit();
			lastsel3 = lastsel2;
			rowDatas=rowData;
			selLineIds=selLineId;
			corpIds=corpId;
			vrsnCdHtmls=vrsnCdHtml;
			selLineName=lineName;
			$("#vrsnCdSelection").val('<option value="'+vrsnCdHtmls+'"'+'>'+vrsnCdHtmls+'</option>');
			$("#vrsnCdSelection").trigger('change');
			// if(flag!="批量修改"){
			// 	$('#allFix').css('visibility','hidden')
			// }else{
			// 	$('#allFix').css('visibility','visible')
            //
			// }
			if(flag!="add"){
				$("#strtusDateStationEnable").attr('readonly',true);
			}
			if(lastsel3=="add"){
				$("#beginTimeLineParameterSet").val(null);
				$("#endTimeLineParameterSet").val(null);
				$("#versionName").val(null);
				$("#strtusDateStationEnable").val(null);
				$("#forAddOrEdit").val("add");
				$("#STNP0002").prop("checked", true);
				$("#STNP0001").prop("checked", false);
				$("#STNP0003").prop("checked", false);

			}else{
				$("#forAddOrEdit").val("edit");

			}
			$("#overStation").val(null);
			$("#stayStation").val(null);
		};
		//批量修改
		$editAll.click(function(){
			 debugger
	    	 //越站
	   		var overStation = $("#overStation").val().trim();
	   		if(overStation){
	   			if(!/^\d+$/.test(overStation)){
	   				$.messager.alert("提示","越站时长输入值不符合，请重新输入正整数");
	   				return;
	   			}
	   			else{
	   				var rowIds =jQuery("#lineParameterSetWorkList").jqGrid('getDataIDs');;
	   				for (var i = 0; i < rowIds.length; i ++) {
	   					jQuery("#lineParameterSetWorkList").jqGrid('setCell',rowIds[i], 'aheadStnDura', overStation);
	   				}
	   			}
	   		}
	   		//滞站
	   		var stayStation = $("#stayStation").val().trim();
	   		if(stayStation){
	   			if(!/^\d+$/.test(stayStation)){
	   				$.messager.alert("提示","滞站时长输入值不符合，请重新输入正整数");
	   				return;
	   			}
	   			else{
					var rowIds =jQuery("#lineParameterSetWorkList").jqGrid('getDataIDs');
	   				for (var i = 0; i < rowIds.length; i ++) {
	   					jQuery("#lineParameterSetWorkList").jqGrid('setCell',rowIds[i], 'stayStnDura', stayStation);
	   				}
	   			}
	   		}

		});

	$("#vrsnCdSelection").on('change',function(){
		$("input[name=parameSet]").prop("checked", false);
		$.ajaxExt({
			url :"/operationReport/getVrsnName.htm",
			data : {"lineId":selLineIds,"vrsnName":vrsnCdHtmls}
		}).done(function(data){
			if($("#forAddOrEdit").val()!="add") {
				$("#beginTimeLineParameterSet").val(data[0].beginTime);
				$("#endTimeLineParameterSet").val(data[0].endTime);
				$("#versionName").val(data[0].vrsnName);
				$("#strtusDateStationEnable").val(data[0].strtusDate);
				$("#startFlag").val(data[0].flag);
				$("#" + data[0].dateType + "").prop("checked", true);

			}

			$lineParameterSetWorkList.grid('setGridParam', {
				page:1,
				datatype: "JSON",
				postData:{"vrsnName":vrsnCdHtmls,"addOrEdit":$("#forAddOrEdit").val(),"lineId":selLineIds}
			}).trigger("reloadGrid");
		});
	});

	window.save= function (callBack) {
		debugger
		nodes=commonFun.getLeftTreeSelectedNodes();
		if(nodes.length!=1){
			$.messager.alert("提示","请选择一条线路查看");
			return;
		}else if(!$("#beginTimeLineParameterSet").val()){
			$.messager.alert("提示","请选择开始时间");
			return;
		}
		else if(!$("#endTimeLineParameterSet").val()){
			$.messager.alert("提示","请选择结束时间");
			return;
		}
		else if(!$("#STNP0001").val()&&!$("#STNP0002").val()&&!$("#STNP0003").val()){
			$.messager.alert("提示","请选择适用时间");
			return;
		}
		else if(!$("#strtusDateStationEnable").val()){
			$.messager.alert("提示","请选择启用日期");
			return;
		}
		else if(!$("#versionName").val()){
			$.messager.alert("提示","请填写版本名称");
			return;
		}
		jQuery('#lineParameterSetWorkList').grid('saveRow',lastsel2);
		var rowDatas= $lineParameterSetWorkList.getRowData();
		if(rowDatas.length>0) {

			var lineType="",stnSn="",stnName="",aheadStnDura="",stayStnDura1="",vrsnCd="",stnCd="";

			for (var i = 0; i < rowDatas.length; i++) {

				lineType += rowDatas[i].lineType + ",";
				stnSn += rowDatas[i].stnSn + ",";
				stnName += rowDatas[i].stnName + ",";
				aheadStnDura += rowDatas[i].aheadStnDura + ",";
				stayStnDura1 += rowDatas[i].stayStnDura + ",";
				vrsnCd += rowDatas[i].vrsnCd + ",";
				stnCd += rowDatas[i].stnCd + ",";
			}
			var vrsnName = $("#versionName").val();
			var beginTime = $("#beginTimeLineParameterSet").val();
			var endTime = $("#endTimeLineParameterSet").val();
			var dateType = $('input[name="parameSet"]:checked').val();
			var strtusDate = $("#strtusDateStationEnable").val();
			if (vrsnCd == ",,,,,,,,,,,,,,,,,,,,") {
				vrsnCd == "";
			}
			var forAddOrEdit = $("#forAddOrEdit").val();
			var result = $.validator($formData);
			if (result) {
				if (lastsel3 == "add") {
					//新增
					$.ajaxExt({
						url: "/operationReport/checkVersion.htm",
						data: {
							"vrsnName": vrsnName,
							"lineId": selLineIds,
							"beginTime": beginTime,
							"endTime": endTime,
							"dateType": dateType,
							"strtusDate": strtusDate,
							"lineType": lineType,
							"stnSn": stnSn,
							"stnName": stnName,
							"aheadStnDura": aheadStnDura,
							"stayStnDura": stayStnDura1,
							"vrsnCd": "",
							"stnCd": stnCd,
							"corpId": corpIds
						}
					}).done(function () {
						$.messager.confirm("提示", "保存成功!", function (r) {
							if (r) {
								callBack();
							}}
						)
						// $lineParameterSetWorkList.grid('setGridParam', {
						// 	page:1,
						// 	datatype: "JSON",
						// 	postData:{"addOrEdit":$("#forAddOrEdit").val(),"lineId":selLineIds}
						// }).trigger("reloadGrid");
					});
				} else {
					//修改
					$.messager.confirm("提示", "是否覆盖  线路:" + selLineName + ", 版本名称:" + vrsnCdHtmls + " 的数据？", function (r) {
						if (r) {
							$.ajaxExt({
								url: "/operationReport/saveStationBehavior.htm",
								data: {
									"vrsnName": vrsnName,
									"lineId": selLineIds,
									"beginTime": beginTime,
									"endTime": endTime,
									"dateType": dateType,
									"strtusDate": strtusDate,
									"lineType": lineType,
									"stnSn": stnSn,
									"stnName": stnName,
									"aheadStnDura": aheadStnDura,
									"stayStnDura": stayStnDura1,
									"vrsnCd": vrsnCd,
									"stnCd": stnCd,
									"corpId": corpIds
								}
							}).done(function () {
								$.messager.confirm("提示", "保存成功!", function (r) {
									if (r) {
										callBack();
									}}
									)

								// this.dialog.close();
								// $lineParameterSetWorkList.grid('setGridParam', {
								// 	page:1,
								// 	datatype: "JSON",
								// 	postData:{"addOrEdit":$("#forAddOrEdit").val(),"lineId":selLineIds}
								// }).trigger("reloadGrid");

							});
						}
					});
				}
			}
		}

	}
});
